﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;

using System.IO;
using System.Data;
using System.Xml;
using System.Web.Security;
using System.Security.Cryptography;
using System.Net;
using System.Web;


namespace Gift.WebApp
{
    /// <summary>
    ///DataEncryption 的摘要说明
    /// </summary>
    public class DataEncryption
    {
        // Fields
        public static string Key = "yonghezhang2012!@#$%^";

        // Methods
        //public string EncryptionMD5(string encryptionString)
        //{
        //    MD5 md = MD5.Create();
        //    byte[] buffer = new byte[0x10];
        //    ASCIIEncoding encoding = new ASCIIEncoding();
        //    return Convert.ToBase64String(md.ComputeHash(encoding.GetBytes(encryptionString)));
        //}

        public string GetString(string base64String)
        {
            return Encoding.Default.GetString(Convert.FromBase64String(base64String.Replace("%2B", "+")));
        }

        //public string MD5Decrypt(string pToDecrypt)
        //{
        //    DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        //    byte[] buffer = new byte[pToDecrypt.Length / 2];
        //    for (int i = 0; i < (pToDecrypt.Length / 2); i++)
        //    {
        //        int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);
        //        buffer[i] = (byte)num2;
        //    }
        //    provider.Key = Encoding.ASCII.GetBytes(Key);
        //    provider.IV = Encoding.ASCII.GetBytes(Key);
        //    MemoryStream stream = new MemoryStream();
        //    CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
        //    stream2.Write(buffer, 0, buffer.Length);
        //    stream2.FlushFinalBlock();
        //    StringBuilder builder = new StringBuilder();
        //    return Encoding.ASCII.GetString(stream.ToArray());
        //}

        //public string MD5Encrypt(string pToEncrypt)
        //{
        //    DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        //    byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
        //    provider.Key = Encoding.ASCII.GetBytes(Key);
        //    provider.IV = Encoding.ASCII.GetBytes(Key);
        //    MemoryStream stream = new MemoryStream();
        //    CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
        //    stream2.Write(bytes, 0, bytes.Length);
        //    stream2.FlushFinalBlock();
        //    StringBuilder builder = new StringBuilder();
        //    foreach (byte num in stream.ToArray())
        //    {
        //        builder.AppendFormat("{0:X2}", num);
        //    }
        //    builder.ToString();
        //    return builder.ToString();
        //}

        //public string RSADecrypt(string decryptString)
        //{
        //    CspParameters parameters = new CspParameters();
        //    parameters.KeyContainerName = "whaben";
        //    RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters);
        //    int num = decryptString.Length / 2;
        //    byte[] rgb = new byte[num];
        //    for (int i = 0; i < num; i++)
        //    {
        //        string str = decryptString.Substring(i * 2, 2);
        //        rgb[i] = Convert.ToByte(str, 0x10);
        //    }
        //    rgb = provider.Decrypt(rgb, true);
        //    return Encoding.ASCII.GetString(rgb);
        //}

        //public string RSAEncrypt(string encryptString)
        //{
        //    CspParameters parameters = new CspParameters();
        //    parameters.KeyContainerName = "whaben";
        //    byte[] buffer = new RSACryptoServiceProvider(parameters).Encrypt(Encoding.ASCII.GetBytes(encryptString), true);
        //    string str = "";
        //    foreach (byte num in buffer)
        //    {
        //        str = str + string.Format("{0:x2}", num);
        //    }
        //    return str;
        //}

        public string ToBase64String(string encryptionString)
        {
            return Convert.ToBase64String(Encoding.Default.GetBytes(encryptionString)).Replace("+", "%2B");
        }

    }
}